/*

****************************************************************************

Summary:

Make programs for PH voting analysis

****************************************************************************

*/

set more off
global prep_data=1
global voting_table=1

if $prep_data==1 {

	cap program drop prep_data
	program define prep_data
	 
	gen proj_code=.
	 replace proj_code=1 if proj_grp=="HR 1995" & proj_code==.
	 replace proj_code=2 if proj_grp=="MA 1995" & proj_code==.
	 replace proj_code=3 if proj_grp=="MA 1998" & proj_code==.
	 replace proj_code=4 if proj_grp=="RC 1998" & proj_code==.
	 replace proj_code=5 if proj_grp=="RT 1995" & proj_code==. 
	 replace proj_code=6 if proj_grp=="RT 1998" & proj_code==.
	 replace proj_code=7 if proj_grp=="SG 1996" & proj_code==.
	 replace proj_code=8 if proj_grp=="WA 1995" & proj_code==.
	 replace proj_code=9 if proj_grp=="WE 1995" & proj_code==. 
	 
	assert proj_code~=.
		
	gen high_demo = (proj_code~=1 & proj_code~=8 & proj_code~=9)
	gen low_demo = 1 if high_demo==0
	replace low_demo = 0 if missing(low_demo)
		 
	gen high_demo_treat = high_demo*treat
	gen low_demo_treat = low_demo*treat
	
	* missing indicators for covariates
	foreach var of varlist male black {
		gen ms_`var' = (`var'==.)
		replace `var' = 0 if missing(`var')
	}
	
	* define a variable indicating "far" away from demo site within a project 
	gen far = 0
	 replace far = 1 if xxbdg=="RT002"
	 replace far = 1 if xxbdg=="RT003"
	 replace far = 1 if xxbdg=="RT024"
	 replace far = 1 if xxbdg=="RT007"
	 replace far = 1 if xxbdg=="RT012"
	 
	 replace far = 1 if xxbdg=="WA065"
	 replace far = 1 if xxbdg=="WA139"
	 
	 replace far = 1 if xxbdg=="SW001"
	 replace far = 1 if xxbdg=="SW003"
	 
	 replace far = 1 if xxbdg=="WE009"
	 replace far = 1 if xxbdg=="WE008"
	 
	gen near = (far==0 & treat==0)
	 tab near far, m
	 
	* create variable for non-partisan
	gen registered_nonpartisan_v2 = (registered_anything==1 & registered_republican==0 & registered_democrat==0) if registered_nonpartisan~=.
	drop registered_nonpartisan
	rename registered_nonpartisan_v2 registered_nonpartisan
	
	end
	
}

if $voting_table==1 {

    cap program drop voting_table
	program define voting_table
	
	/* program local key:

		 `1' will be the data restriction, for example, if we want all males, this will be set with the `1' entry in the program
		 `2' will be the variables of interest used below
		 `3' will be the name specified for the output file
		 `4' will be the age restriction
		 
		 notes:
		  $cov is set outside this program and used in the regressions
		  $age_range is set outside this program and used in the regressions

	 */ 
	 
	prep_data

	* Obtain sample
	keep if `1'
	 // first argument of the program select the sample to keep
	 	 
	* Set log file 
	cap log close
	log using "${logs}table_`3'_pooled_xsec.txt", text replace 
	
	display "Sample: `1' -- POOLED CROSSSECTION SPECIFICATIONS BELOW"
	display "Outcomes (global specified): `3'"
	display "Restriction on age: `4'"
	 
	*** Create Table
	file close _all
	file open table using "${output}table_`3'_pooled_xsec.tex", write replace
	*file write table "var,cm,all,,N_xsec" _n

	foreach var of varlist $`2' {
	 // loop over each specific variable
	 
	    display "`var'"
		
		* Headers
		if "`var'"=="voted_general" {
		     
		file write table "\textbf{Voting:}"
		
		file write table "\\ \\[-1.5ex]"
		file write table _n 
		 // end row
		 
		}
		
		* Headers
		if "`var'"=="registered_anything" {
		     
		file write table "\textbf{Registration:}"
		
		file write table "\\ \\[-1.5ex]"
		file write table _n 
		 // end row
		 
		}
		
	    * variable names (all possibilities)
		if "`var'"=="voted_ever" {
			file write table "\quad Ever Voted, Any "
			}
			
		if "`var'"=="voted_general" {
			file write table "\quad Ever Voted, General "
			}
			
		if "`var'"=="voted_primary_ever" {
			file write table "\quad Ever Voted, Primary " 
			}
			
		if "`var'"=="pres_voted_share" {
			file write table "\quad Share of Pres. Elections Voted "
			}
			
		if "`var'"=="voted_share" {
			file write table "\quad Share of General Elections Voted "
			}
			
		if "`var'"=="general_2016" {
		    file write table "\quad Voted General, 2016 " 
			}
		
		if "`var'"=="general_2012" {
		    file write table "\quad Voted General, 2012 " 
			}
			
		if "`var'"=="general_2008" {
		    file write table "\quad Voted General, 2008 " 
			}

		if "`var'"=="general_2004" {
		    file write table "\quad Voted General, 2004 " 
			} 

		if "`var'"=="voted_by_age24" {
			file write table "\quad Voted by Age 24 "
			}
			
		if "`var'"=="voted_after_age24" {
			file write table "\quad Voted After Age 24 "
			}
		
		if "`var'"=="registered_anything" {
			file write table "\quad Registered "
			}
			
		if "`var'"=="registered_nonpartisan" {
			file write table "\quad Registered, Non-partisan "
			}
			
		if "`var'"=="registered_republican" {
			file write table "\quad Registered, Republican "
			}
		
		if "`var'"=="registered_democrat" {
			file write table "\quad Registered, Democrat "
			}
			
		* all
		sum `var' if treat==0 & $age_range & `4' & tag==1 // control group mean
		file write table "&" (string(r(mean),"%15.3fc"))

		xi: reg `var' treat $cov if $age_range & `4' & tag==1, cluster(xxbdg)

		test treat
		local p_val = r(p)
		
		local sig ""
		if `p_val'<.1 local sig "*"
		if `p_val'<.05 local sig "**"
		if `p_val'<.01 local sig "***"
		 
		file write table "&" (string(_b[treat],"%15.3fc")) "`sig'"
		
		* display effect size (recorded in log)
		display "effect size:"
		qui sum `var' if treat==0 & $age_range & `4' & tag==1 // control group mean
		display _b[treat]/r(mean)
		 
		*file write table "&"
		
		* # persons
		qui cap xi: reg `var' treat $cov if $age_range & `4' & tag==1, cluster(xxbdg)
		egen temp = tag(chdhsid_201212) if e(sample)==1
		count if temp==1
		file write table "&" (string(r(N),"%15.0gc"))
		drop temp
		
		file write table " \\"

		file write table _n // end means/estimates row
		
		* Now compute standard errors
		
		file write table "&"
		
		xi: reg `var' treat $cov if $age_range & `4' & tag==1, robust cluster(xxbdg)
		file write table "& (" (string(_se[treat],"%15.3fc")) ")" 
		
		file write table " \\"
		 
		file write table _n // end standard error row

	} // end outcome loop
	
	file close table // close table
	display "END"
	cap log close 
	
	end // end program definition

} // end program bracket